package com.suray.system.run.data.mysql.device.offline;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.system.run.data.mysql.device.entity.ErrorDesDB;
import com.suray.system.run.data.mysql.device.entity.OffLineDesDB;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class OffLineDesDBServiceImpl extends ServiceImpl<OffLineDesDBMapper, OffLineDesDB> implements OffLineDesDBService {

    @Resource
    OffLineDesDBMapper offLineDesDBMapper;

    @Override
    public OffLineDesDB findOffLineDesDBBySeries(String series) {
        LambdaQueryWrapper<OffLineDesDB> eq = new QueryWrapper<OffLineDesDB>().lambda()
                .eq(OffLineDesDB::getSeries, series)
                .isNull(OffLineDesDB::getOffLineEndDate);

        List<OffLineDesDB> offLineDesDBS = this.list(eq);
        if (offLineDesDBS.size() == 0) {
            return null;
        }
        return offLineDesDBS.get(0);
    }

    public List<OffLineDesDB> getOffLineList(String series, Date startTime, Date endTime) {
        QueryWrapper<OffLineDesDB> eq = new QueryWrapper<>();

        eq.lambda().eq(OffLineDesDB::getSeries, series);

        if(startTime != null){
            eq.lambda().ge(OffLineDesDB::getOffLineStartDate, startTime);
        }
        if(endTime != null){
            eq.lambda().le(OffLineDesDB::getOffLineEndDate, endTime);
        }

        return this.list(eq);
    }
}
